$(function () {
    pageLayout();
    jerichoTabInit();
    // menu.getMenu();
});

/**
 * 页面布局
 */
function pageLayout() {
    //页面高度
    var bodyViewHeight = window.document.body.clientHeight;
    //页面宽度
    var bodyViewWidth = window.document.body.clientWidth;
    //上侧导航栏高
    var brandHeight = window.document.getElementById("navigation").offsetHeight;
    var contentHeight = bodyViewHeight - brandHeight;
    //左侧菜单栏高度设置
    document.getElementById("left").style.height = contentHeight +"px";
    var menubarWidth = window.document.getElementById("left").offsetWidth;
    //主体内容宽/高设置
    document.getElementById("main-content").style.paddingLeft = menubarWidth+2 + "px";
    document.getElementById("main-content").style.height = contentHeight +"px";
}

/**
 * jerichoTabInit 初始化
 */
function jerichoTabInit() {
    $.fn.initJerichoTab({
        renderTo: '#main-content', uniqueId: 'jumper-tab',
        tabs: [], loadOnce: true, tabWidth: 110, contentCss: { 'height': 94+"%" }
    });
}

/**
 * 打开新页面
 * @param url
 * @param name
 */
function addTab(url, name){
    $.fn.jerichoTab.addTab({
        tabFirer: null,
        title: name,
        closeable: true,
        data: {
            dataType: 'iframe',
            dataLink: url
        }
    }).loadData(true);
}

var menu = {

    getMenu: function () {
        $.ajax({
            type: "get",
            dataType:'json',
            cache: false,
            url: ctx + "/security/userMenu",
            success: function(data){
                if(data) {
                    var _html = menu.createMenuPage(data);
                    console.log(_html);
                    $("#menu-bar").append(_html);
                }
            },
            error: function(){
                throw 'occur error when try getting user menus';
            }
        });
    },

    createMenuPage: function(menus) {
        var menuHtml = '';
        for(var i=0; i<menus.length; i++) {
            var menu = menus[i];
            var hasChild = false;
            if(menu.subResource.length > 0) {
                hasChild = true;
            }
            var menuPage = [];
            menuPage.push('<div class="accordion-group" id="menu-'+  menu.id +'">');
            menuPage.push('<div class="accordion-heading">');
            if(hasChild) {
                menuPage.push('<a class="accordion-toggle" data-toggle="collapse" ' +
                    'data-target="#'+ menu.id +'" title="'+ menu.name +'">');
            } else {
                menuPage.push('<a class="accordion-toggle" href="javascript:void(0)" ' +
                    'onclick="addTab(menu.url, menu.name)" title="'+ menu.name +'">');
            }
            menuPage.push('<span>'+ menu.name +'</span></a>');
            menuPage.push('</div>');
            if(hasChild) {
                menuPage.push('<div id="'+ menu.id +'" class="accordion-body collapse">');
                menuPage.push('<div class="accordion-inner">');
                menuPage.push('<ul>');
                var childMenuSoruce = menu.subResource;
                for(var k=0; k< childMenuSoruce.length; k++) {
                    var childMenu = childMenuSoruce[k];
                    if(childMenu.subResource.length > 0) {
                        menuPage.push('<li>');
                        var childMenuGroup = this.createMenuPage(childMenu.subResource);
                        menuPage.push(childMenuGroup);
                        menuPage.push('</li>');
                    } else {
                        menuPage.push('<li><a onclick="addTab(\''+ ctx+ childMenu.url + '\', \''+ childMenu.name +'\')">'+childMenu.name+'</a></li>');
                    }
                }
                menuPage.push('</ul>');
            }
            menuPage.push('</div>');
            menuHtml += menuPage.join('');
        }
        return menuHtml;
    }

};
